<{include file="Order/top.html" }>
</head>
<body>
<{include file="wap_index_top.html" }>
<div class="xy-panel">
    <div class="xy-panel-top">
        <div class="xy-titlebar">
            <div class="xy-titlebar-left">
                <a class="insertBefore_left_arrow_gray" href="javascript:void(0);"></a>
            </div>
            <div class="xy-titlebar-title">支付信息</div>
            <div class="xy-titlebar-right">
                <div class="spacer"></div>
            </div>
        </div>
    </div>
    <div class="xy-panel-content" style="bottom:0;">
        <div class="xy-row vDirectionRow">
            <h2 class="title title-2" style="margin-bottom:1rem;">
                <img src="<{$preuploadcurl}><{$data.imgList}>">
                <dl>
                    <h3><{$orderInfo.orderTitle}></h3>
                    <{if $data.showListTime}>
                        <span>活动时间：<{$data.showListTime}></span>
                    <{else}>
                        <span>类型：<{$data.deliveryType}></span>
                    <{/if}>
                    <span>市场参考价：<{$orderInfo.marketPrice}>元</span>
                </dl>
            </h2>
            <ul class="zfxx_box">
                <{if $orderInfo.userName}>
                    <li class="zfxx_list">
                        <span>用户姓名：</span><span><{$orderInfo.userName}></span>
                    </li>
                <{/if}>
                <{if $orderInfo.phone}>
                    <li class="zfxx_list">
                        <span>联系电话：</span><span><{$orderInfo.phone}></span>
                    </li>
                <{/if}>
                <{if $orderInfo.address}>
                    <li class="zfxx_list map_icon">
                        <span>用户地址：</span><span style="width:75%;"><{$orderInfo.address}></span>
                    </li>
                <{/if}>
            </ul>
            <div class="qrzf_btn">
                <h3 class="qrzf_topic">支付金额：人民币<{$orderInfo.pay}>元</h3>
                <{if $userInfo.money > 0}>
                    <div class="radio_item"><input type="checkbox" name="balance" value="balance" /><span class="zf">余额支付</span><span class="je"><{$userInfo.money}>元</span></div>
                    <{if ($orderInfo.pay - $userInfo.money) > 0}>
                        <div class="ce">还需支付<font><{$orderInfo.pay-$userInfo.money}></font>元</div>
                    <{/if}>
                <{/if}>
                <div class="radio_item"><input type="checkbox" name="buy" value="payment" checked/><span class="zf">微信支付</span><span class="je" style="color:#37cbbb;"><font id="payMoney"><{$orderInfo.pay}></font>元</span></div>
            </div>
            <div class="ui-button ui-button-2">
                <a href="javascript:void(0);" id="payment">确认支付</a>
            </div>
        </div>
    </div>
</div>
<{include file="signBody.html"}>

<script type="text/javascript">
    var balance = toDecimal(<{$userInfo.money}>);
    var pay = toDecimal(<{$orderInfo.pay}>);
    var payFlag = true;
    var pay_money = 0;
    var pay_balance = 0;
    var apiMsg = {};

    function onCheckedClick(){
        pay_money = pay_balance = 0;
        var _that = this;
        var payMoney = pay;
        payFlag = false;
        $('input[type="checkbox"]:checked').each(function () {
            var isType = $(this).val();
            if(isType == 'balance'){
                pay_balance = pay>balance?balance:pay;
                payMoney = payMoney-balance;
                if(payMoney>0) $(".ce font").html(payMoney);
                if(payMoney<=0) payFlag = true;
            }
            payMoney = payMoney<0?0:payMoney;
            $("#payMoney").html(payMoney);
            if(isType == 'payment'){
                if(payMoney<=0) $(this).removeAttr("checked");
                if(payMoney>0) $("#payMoney").html(payMoney);
                payFlag = true;
            }
            pay_money = payMoney;
        });
    }

    $(function () {
        onCheckedClick();
        $("input[type=checkbox]").on('click',onCheckedClick);
        var loadObj;
        var btnFlag = 1;
        $("#payment").on('click', function () {
            if(!payFlag){
                layer.msg('请选择支付方式');
                return false;
            }

            if(!pay_money && !pay_balance){
                return false;
            }
            loadObj = layer.load(2);
            if(!btnFlag) return false;
            btnFlag = 0;
            $.getJSON("<{U('Order/getPay')}>", {id:<{$orderInfo.id}>, uid:<{$orderInfo.uid}>, pay_money:pay_money, pay_balance:pay_balance}, function(data){
                layer.close(loadObj);
                btnFlag = 1;
                if(data.status!=0){
                    if(data.status == 3){
                        window.location.href = data.url;
                        return false;
                    }
                    layer.msg(data.msg);
                    if(data.status == 2){
                        window.history.go(-1);
                        return false;
                    }
                }else{
                    if(pay_balance>0 && pay_money == 0){
                        checkOrder();
                    }else{
                        apiMsg = JSON.parse(data.data);
                        callpay();
                    }
                }
            });
        });
    });

    //重新
    function checkOrder() {
        loadObj = layer.load(2);
        $.getJSON("<{U('Order/checkOrderStatus')}>", {id:<{$orderInfo.id}>, uid:<{$orderInfo.uid}>}, function(data){
            layer.close(loadObj);
            if(data.status!=0){
                layer.msg(data.msg);
            }else{
                window.location.href = data.url;
            }
        });
    }


    function callpay()
    {
        if(!canpay){
            return false;
        }
        var data={
            "success":function(){checkOrder();},
            "fail":function(res){
                layer.msg("支付失败，请重试或联系客服");
            },
            'timestamp':apiMsg.timeStamp,
            'nonceStr':apiMsg.nonceStr,
            'package':apiMsg.package,
            'signType':apiMsg.signType,
            'paySign':apiMsg.paySign

        }
        wx.chooseWXPay(data);

        return true;

    }
</script>
</body>
</html>

